#include "stdafx.h"

BOOL ClearSurface(LPDIRECTDRAWSURFACE7 surf, int iColor)
{
	if (!surf)
		return 0;
	DDBLTFX bltfx;
	ZeroMemory(&bltfx,sizeof bltfx);
	bltfx.dwSize = sizeof(bltfx);
	bltfx.dwFillColor = iColor;

	HRESULT r = surf->Blt(0,0,0,DDBLT_COLORFILL | DDBLT_WAIT, &bltfx);
	if (r == DD_OK)
		return TRUE;
	else
		return FALSE;
}

DWORD GetRGBColor(DDPIXELFORMAT *ftpixel, DWORD R, DWORD G, DWORD B)
{
	if (R != 0) R = (1024 / R); else R = 1025;
	if (G != 0) G = (1024 / G); else G = 1025;
	if (B != 0) B = (1024 / B); else B = 1025;
	DWORD r =	(ftpixel->dwRBitMask / R & ftpixel->dwRBitMask) +
				(ftpixel->dwGBitMask / G & ftpixel->dwGBitMask) +
				(ftpixel->dwBBitMask / B & ftpixel->dwBBitMask);
	return	r;
}

HRESULT DXDDInit(IDirectDraw7 **ppiDD,
				 IDirectDrawSurface7 **primsurf, IDirectDrawSurface7 **backsurf,
				 HWND hWnd, SIZE sizScreen, WORD iColors, BOOL *bFullScreen)
{
	// Checking fullscreen
	int iRes;
	if 
		(*bFullScreen) iRes = IDYES;
	else
		iRes = MessageBox(hWnd,L"Do you want to use fullscreen?",L"Fullscreen",MB_YESNO | MB_ICONQUESTION);
	HRESULT hr;
	hr = DirectDrawCreateEx(NULL,(VOID**) ppiDD,IID_IDirectDraw7,NULL);
	if (FAILED(hr))
		return hr;

	if (iRes == IDYES)
	{
		//Fullscreen
		(*bFullScreen) = TRUE;
		SetWindowLong(hWnd,GWL_STYLE,WS_POPUP);
		(*ppiDD)->SetCooperativeLevel(hWnd,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX);
		(*ppiDD)->SetDisplayMode(sizScreen.cx,sizScreen.cy,iColors,0,0);
	}
	else
	{
		//Window
		(*bFullScreen) = FALSE;
		RECT wndRect;
		RECT clientRect;
		GetWindowRect(hWnd,&wndRect);
		GetClientRect(hWnd,&clientRect);
		int iWinWidth = sizScreen.cx + (wndRect.right - wndRect.left) - (clientRect.right-clientRect.left);
		int iWinHeight = sizScreen.cy + (wndRect.bottom - wndRect.top) - (clientRect.bottom - clientRect.top);
		MoveWindow(hWnd,wndRect.left,wndRect.top,iWinWidth,iWinHeight,TRUE);
		(*ppiDD)->SetCooperativeLevel(hWnd,DDSCL_NORMAL);
	}

	DDSURFACEDESC2 desc;
	ZeroMemory(&desc,sizeof(desc));
	desc.dwSize = sizeof(desc);

	if (iRes == IDYES)
	{
		//FullScreen
		desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
		desc.dwBackBufferCount = 1;
		desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
	}
	else
	{
		desc.dwFlags = DDSD_CAPS;
		desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
	}

	hr = (*ppiDD)->CreateSurface(&desc,primsurf,0);
	
	if (iRes == IDYES)
	{
		DDSCAPS2 surfcaps;
		ZeroMemory(&surfcaps,sizeof(surfcaps));
		surfcaps.dwCaps = DDSCAPS_BACKBUFFER;
		hr = (*primsurf)->GetAttachedSurface(&surfcaps,backsurf);
		if (hr !=DD_OK)
			return hr;
		ClearSurface(*primsurf,4545);
		ClearSurface(*backsurf,4545);
	}
	else
	{
		//Window
		//Clipper
		LPDIRECTDRAWCLIPPER pddClip;
		(*ppiDD)->CreateClipper(0,&pddClip,0);
		pddClip->SetHWnd(0,hWnd);
		(*primsurf)->SetClipper(pddClip);

		ZeroMemory(&desc, sizeof(desc));
		desc.dwSize = sizeof(desc);
		desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
		desc.dwHeight = sizScreen.cy;
		desc.dwWidth = sizScreen.cx;
		desc.ddsCaps.dwCaps= DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
		hr = (*ppiDD)->CreateSurface(&desc, backsurf,0);
		if (hr!= DD_OK)
			return hr;
		//ClearSurface(*primsurf,0);
	}
	return DD_OK;
}